using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Pool;
namespace DefaultNamespace.Pool
{
    public class VFXManager:MonoBehaviour
    {
        [SerializeField]private ParticleSystem vfxPrefab;
        private IObjectPool<ParticleSystem> vfxPool;
        private void Awake()
        {
            vfxPool = new ObjectPool<ParticleSystem>(
                createFunc:()=>Instantiate(vfxPrefab),
                actionOnGet:(vfx)=>vfx.Play(),
                actionOnRelease:(vfx)=>vfx.Stop(),
                actionOnDestroy:(vfx)=>Destroy(vfx.gameObject),
                collectionCheck:true,
                maxSize:20);
        }
        public void PlayVFX(Vector3 position)
        {
            var vfx = vfxPool.Get();
            vfx.transform.position = position;
            StartCoroutine(ReturnVFXAfterPlaying(vfx));
        }
        private IEnumerator ReturnVFXAfterPlaying(ParticleSystem vfx)
        {
            yield return new WaitWhile(() => vfx.isPlaying);
            vfxPool.Release(vfx);
        }
    }
}